
package controle;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Aluno;
import dao.AlunoDao;
import javax.swing.JOptionPane;


@WebServlet(name = "ControllerAlterar", urlPatterns = {"/ControllerAlterar"})
public class ControllerAlterar extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            /*out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControllerAlterar</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControllerAlterar at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");*/
        } finally {            
            out.close();
        }
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        Aluno aluno =  new Aluno();
        AlunoDao alunoDao =  new AlunoDao();
        
        aluno = alunoDao.consultarPorId(Integer.valueOf(request.getParameter("txtid")));
        aluno.setAlunome(request.getParameter("txtnome"));
        
        alunoDao.alterar(aluno);
        
        JOptionPane.showMessageDialog(null, "Alteração realizada com sucesso!");
        
        request.getRequestDispatcher("ListarAluno.htm");        
    }

    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
